import grails.util.GrailsUtil

import com.pijunn.common.*
import com.pijunn.security.*
import com.pijunn.users.*

class BootStrap {
	
    def init = { servletContext ->
		
		switch(GrailsUtil.environment){
	      case "development":
			def userRole = SecRole.findByAuthority('ROLE_USER') ?: new SecRole(authority: 'ROLE_USER').save(failOnError: true)
			def adminRole = SecRole.findByAuthority('ROLE_ADMIN') ?: new SecRole(authority: 'ROLE_ADMIN').save(failOnError: true)
			
			def address = new Address(address1: '1316 Durango Ridge Rd', address2: '', city : 'Durango', state: 'CO', zipCode: '81301', country: 'US')
	
			def adminUser = AppUser.findByUsername('admin') ?: new AppUser(
	                username: 'admin',
					firstName: 'App', lastName: 'User',
					contactEMail: 'mrosent@gmail.com',
					contactCellPhone: '', contactOtherPhone: '', contactSkypeId: '',
					mailingAddress: address,
	                password: 'landshark', 
					createTime: new Date(),
	                enabled: true).save(failOnError: true)
	 
			if (!adminUser.authorities.contains(adminRole)) {
				SecUserSecRole.create adminUser, adminRole
			}
	      break
	
	      case "production":
	      break
	    }
	
    }
    def destroy = {
    }
}
